package dao;

import dao.imp.IDeptDao;
import entity.Dept;
import entity.Employee;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import util.MysqlConnUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: personnel_management_system
 * @description: dept opreation
 * @author: Mr.Sun
 * @create: 2020-05-25 14:12
 **/

public class DeptDao implements IDeptDao {
    private JdbcTemplate template = new JdbcTemplate(MysqlConnUtil.getDataSource());

    public DeptDao() throws Exception {
    }

    @Override
    public boolean insert(Dept dept) {
        boolean res = false;
        String sql = "INSERT INTO dept(dname) VALUES(?)";

        int count = template.update(sql,dept.getDname());
        res = count == 1;
        return res;

    }

    @Override
    public boolean update(Dept dept) {

        boolean res = false;
        String sql = "UPDATE dept SET deptno = ? , dname = ? WHERE deptno = ?";
        int count = template.update(sql,dept.getDeptno(),dept.getDname(),dept.getDeptno());
        res = count == 1;
        return res;

    }

    @Override
    public boolean delete(Dept dept) {
        boolean res =false;
        String sql = "DELETE FROM dept WHERE deptno = ?";
        int count = template.update(sql,dept.getDeptno());
        return count == 1;
    }

    @Override
    public List<Dept> selectAll() {
        List<Dept> depts = new ArrayList<>();
        String sql ="select * from deptnum";
        depts = template.query(sql,new BeanPropertyRowMapper<Dept>(Dept.class));
        return depts;
    }

    @Override
    public List<Dept> select(Dept dept) {
        return null;
    }
}
